/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ernie.server.context;

import graph.autodrawer.AutoDrawer;

import java.io.IOException;

import misc.HTMLStringStream;
import misc.HttpStream;
import net.sourceforge.javajson.JsonArray;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

/**
 * 
 * @author Frelle
 */
public class MyStocksContext implements HttpHandler {
    private final AutoDrawer autoDrawer;

    public MyStocksContext(AutoDrawer autoDrawer) {
        this.autoDrawer = autoDrawer;
    }

    @Override
    public void handle(HttpExchange he) throws IOException {
        he.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
        JsonArray stocks = new JsonArray();
        for (String stock : autoDrawer.getDrawedStockNames()) {
            stocks.add(stock);
        }

        HttpStream hss = new HTMLStringStream(he);
        hss.write(stocks.toString());
        hss.close();
    }
}
